package com.qf.ym.web;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.ym.MyUtils;
import com.qf.ym.entity.AdminEntity;
import com.qf.ym.entity.ResponseResult;
import com.qf.ym.finalstr.FruitsFinal;
import com.qf.ym.pojo.AdminPojo;
import com.qf.ym.pojo.AuthPojo;
import com.qf.ym.pojo.RolePojo;
import com.qf.ym.service.AdminService;
import com.qf.ym.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
public class AdminController {

    @Autowired
    AdminService adminService;

    @Autowired
    RoleService roleService;



    //跳转到登录界面
    @RequestMapping("/jump")
    public String jumpLogin() {

        /*return "welcome";*/
        return "adminlogin";
    }
    //管理员登录
    @RequestMapping("adminlogin")
    public ModelAndView AdminLogin(AdminEntity ae, HttpServletRequest request){
        ModelAndView mv;

        //登录用户名密码非空校验
        if (ae==null){
            mv=new ModelAndView("fail");
            mv.addObject("errorMsg","系统异常请稍后再试");
            return mv;
        }
        if (MyUtils.isEmpty(ae.getAacount())){
            mv=new ModelAndView("adminlogin");
            mv.addObject("erroMsg","请输入用户名");
            return mv;
        }
        if (MyUtils.isEmpty(ae.getApass())){
            mv=new ModelAndView("adminlogin");
            mv.addObject("erroMsg","请输入密码");
            return mv;
        }
        //对密码进行MD5加密
        ae.setApass(MyUtils.Md5Jm(ae.getApass()));
        //执行登录
        AdminPojo adminPojo = adminService.adminLogin(ae);

        if(adminPojo!=null){//登录成功
            request.getSession().setAttribute("adminPojo",adminPojo);
            //获取当前用户所有权限
            List<RolePojo> roles = adminPojo.getRoles();//用户角色信息
            Set<AuthPojo> auths = new HashSet<>();
            for(RolePojo rp:roles){//吧一个角色的全部权限作为一个集合存入set集合中作为一个元素
                auths.addAll(rp.getAuths());
            }

            //获取用户所有菜单
            Set<AuthPojo> menu = new HashSet<>();//当前用户所有的权限菜单

            for(AuthPojo ap:auths){
                if(ap.getAutype()==1){//菜单栏
                    menu.add(ap);

                }
            }
            //遍历用户所有的权限
            StringBuilder strAuths = new StringBuilder();//以字符串的方式存放用户所有的权限
            for(AuthPojo ap:auths){
                strAuths.append(ap.getAupath());
            }
            request.getSession().setAttribute("strAuths",strAuths);//用户权限的字符串
            request.getSession().setAttribute("auths",auths);//某某用户所有的权限
            request.getSession().setAttribute("menu",menu);//用户所有的菜单

            //


            mv=new ModelAndView("index");
            return mv;
        }else {//登录失败
            request.setAttribute("errorMsg","用户名或者密码错误");
            mv=new ModelAndView("adminlogin");
            return mv;
        }
    }

    //管理员表查询
    @RequestMapping("adminList")
    public String adminList(AdminEntity ae, HttpServletRequest req){
        //分页两个重要参数，当前页和页面数据条数
        PageHelper.startPage(ae.getPageNum(),ae.getPageSize());
        //查询用户列表，根据条件
        List<AdminPojo> adminPojos = adminService.adminList(ae);
        //分页完成的数据,分页插件
        PageInfo<AdminPojo> pageInfo = new PageInfo<AdminPojo>(adminPojos);
        //返回前台
        req.setAttribute("pageInfo",pageInfo);
        return "adminlist";
    }

    //删除管理员用户
     @RequestMapping("/delAdmin")
     @ResponseBody
    public ResponseResult delAdmin(Integer aid){
         ResponseResult result = new ResponseResult();
         boolean fg=adminService.delAdmin(aid);
         if(fg){
             result.setResult(FruitsFinal.SUCCESS_RESULT);
         }else {
             result.setResult(FruitsFinal.FILED_RESULT);
             result.setResultMsg("删除失败");
         }
         return result;
     }
        //根据ids进行批量删除
     @RequestMapping("/delAdminByids")
     @ResponseBody
     public ResponseResult delAdminByids(String aids){
         ResponseResult result = new ResponseResult();
         boolean rs=adminService.delAdminByids(aids);
         if(rs){
             result.setResult(FruitsFinal.SUCCESS_RESULT);
         }else {
             result.setResult(FruitsFinal.FILED_RESULT);
             result.setResultMsg("删除失败");
         }
         return result;
     }
     //查询多有角色信息
    @RequestMapping("jumpAdd")
    public String queryRoles(HttpServletRequest request){
        List<RolePojo> rolePojos = roleService.queryRoles(null);
        request.setAttribute("rolePojos",rolePojos);
        return "adminadd";
    }
    //添加用户角色的绑定

    @RequestMapping("adminAdd")
    public String adminAdd(AdminEntity ae){
        ae.setApass(MyUtils.Md5Jm(ae.getApass()));
        String a=ae.getAname();
        boolean bs = adminService.adminAdd(ae);
        if(bs){
            return "addsuccess";
        }
        return "adminadd";
    }

    //跳转到编辑页面
    @RequestMapping("jumpEdit")
    public String jumpEdit(HttpServletRequest request,int aid){
        //查询所有角色的信息
        List<RolePojo> rolePojos = roleService.queryRoles(null);
        request.setAttribute("rolePojos",rolePojos);
        //根据id查询当前用户的信息以及角色信息
        AdminEntity adminEntity = new AdminEntity();
        adminEntity.setAid(aid);
        AdminPojo adminPojo=adminService.adminQuery(adminEntity);
        request.setAttribute("adminPojo",adminPojo);

        return "adminedit";
    }
    //修改用户信息
    @RequestMapping("editAdmin")
    public String editAdmin(AdminEntity ae,HttpServletRequest request){
         boolean result  =   adminService.editAdmin(ae);
         if(result){
             return "addsuccess";
         }
         request.setAttribute("msg","编辑失败，请重试");
         return "adminedit";
    }

    //  启用，禁用
    @RequestMapping("qiyongAdmin")
    @ResponseBody
    public ResponseResult qiyongAdmin(AdminEntity ae){
        ae.setAstatus(1);
        boolean result =adminService.qiyongAdmin(ae);

        ResponseResult responseResult = new ResponseResult();
        if(result){
            responseResult.setResult(FruitsFinal.SUCCESS_RESULT);
        }else {
            responseResult.setResult(FruitsFinal.FILED_RESULT);
            responseResult.setResultMsg("系统异常，请稍后再试");
        }
        return responseResult;
    }
    //首页的跳转
    @RequestMapping("welcome")
    public String welcome(){
        return "welcome";
    }

    //角色管理页面
    @RequestMapping("adminrole")
    public String adminRole(HttpServletRequest request){

        PageHelper.startPage(1,5);
        //查询所有角色的信息
        List<RolePojo> roleList = roleService.queryRoles(null);

        PageInfo<RolePojo> pageInfo = new PageInfo<RolePojo>(roleList);
        request.setAttribute("pageInfo",pageInfo);

        return "adminrole";
    }



}
